package com.wx.app.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.newemptyapplication.R;
import com.wx.app.mylib.app.BaseActivity;
import com.wx.app.mylib.utils.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class PermissionActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    private static final int RC_CAMERA_PERM = 123;
    private static final int RC_LOCATION_CONTACTS_PERM = 124;

    @BindView(R.id.btn_camera)
    Button btnCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        ButterKnife.bind(mActivity);
    }

    @OnClick(R.id.btn_camera)
    void openCamera() {
        getPermission();
    }

    @AfterPermissionGranted(RC_CAMERA_PERM)
    private void getPermission() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
            // 已经获取权限，进行打开相机的操作
            Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this, getString(R.string.camera),
                    RC_CAMERA_PERM, Manifest.permission.CAMERA);
        }
    }

    //同时获取两个权限
    @AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM)
    public void locationAndContactsTask() {
        String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Have permissions, do the thing!
            Toast.makeText(this, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            EasyPermissions.requestPermissions(this, "需要获取定位和联系人权限，请打开设置，否则程序无法正常运行！",
                    RC_LOCATION_CONTACTS_PERM, perms);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        LogUtil.i("用户已同意获取相机权限！");
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, getString(R.string.rationale_ask_again), R.string.setting, R.string.cancel, perms);
    }
}
